package com.cek.tdrive.login;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;

import com.cek.tdrive.R;
import com.cek.tdrive.constant.AppConstant;
import com.cek.tdrive.home.HomeScreenActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity implements OnClickListener{
	private EditText firstName;
	private EditText lastName;
	private EditText emailId;
	private EditText password;
	private EditText confirmPassword;
	private EditText phone;
	private Context context;
	private Button  submit;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this;
		submit=(Button)findViewById(R.id.submit);
		submit.setOnClickListener(this);
		firstName=(EditText)findViewById(R.id.firstname);
		lastName=(EditText)findViewById(R.id.lastname);
		emailId=(EditText)findViewById(R.id.emailid);
		password=(EditText)findViewById(R.id.password);
		confirmPassword=(EditText)findViewById(R.id.confirmPassword);
		phone=(EditText)findViewById(R.id.phone); 
	}

	public void onClick(View v) {
		int id=v.getId();
		if(id==R.id.submit) {
			String fName=firstName.getEditableText().toString();
			String lName=lastName.getEditableText().toString();
			String email=emailId.getEditableText().toString();
			String pwd=password.getEditableText().toString();
			String confirmpassword=confirmPassword.getEditableText().toString();
			String phonetxt=phone.getEditableText().toString();
			if (fName.equals("")||lName.equals("")||email.equals("")||pwd.equals("")||confirmpassword.equals("")||phonetxt.equals("")) {
				Toast.makeText(context, "please complete the data",Toast.LENGTH_SHORT).show();
			}
			if(pwd.equals(confirmpassword)) {
				Toast.makeText(context, "successfully Registered", Toast.LENGTH_SHORT).show();
				registerUser(fName,lName,pwd, email, phonetxt);
			    finish();
				
			} else {
				Toast.makeText(context, "password mismatch", Toast.LENGTH_SHORT).show();
			}
				
			}
				
		}	
	private boolean registerUser(String userName,String lastName,String password,String email,String phone) {
			System.out.println("username="+userName);
			System.out.println("password="+password);
			String loginPath=AppConstant.REGISTER_PATH+"?firstname="+userName+"&lastname="+lastName+"&password="+password+"&emailid="+email+"&phoneno="+phone;
			System.out.println("loginpath="+loginPath);
			HttpGet request= new HttpGet(loginPath);
			try { 
				HttpResponse  response=AppConstant.HTTP_CLIENT.execute(request);
				HttpEntity entity = response.getEntity();
				String respStr = EntityUtils.toString(entity);
				System.out.println("respStr = "+respStr);
				if(respStr.contains("true")) {
					return true;
				}
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) { 
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			return false;	
		}
}
